<?php

namespace App\Http\Controllers;

use App\Models\Department;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Resources\Department as DepartmentResource;

class DepartmentController extends Controller
{
    //
    public function all(Request $request)
    {
        if ($request->parent_id) {
            $departments = Department::where('parent_id', '=', $request->parent_id)->get();
        } else {
            $departments = Department::all();
        }
        return DepartmentResource::collection($departments);
    }

    public function tree(Request $request)
    {
        $departments = Department::all();
        return DepartmentResource::collection($departments);
        //return DB::table('departments')->select('id','name','parent_id')->get();
    }
}
